// pages/Index/userStatistics/userStatistics.js
const controllerOrder=require("../../controllers/controllerOrder").controller
const controllers = require('../../controllers/controllerIndex.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // status:-1,
    time:false,	 
    pay: false,
    activeIndex:0,
    tabs: [
      {
        name: "今日新用户",
        vip: 0,
      },
      {
        name: "今日新会员",
        vip: 2,//是否vip
      },
      {
        name: "总用户",
        vip: "",//是否vip
      },
      {
        name: "总会员",
        vip: "",//是否vip
      }
    ],
    typeList:["手机号","昵称"],
    typeName:"手机号",
    index:1,
    inputText:'',
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210414/831918901884354560.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210414/831919079068532736.png",
    store:"",
    startTime:"",
    endTime:"",
    pageNumber:1,
    lastPage:false
  },

  //跳转提现文案设置
  toSetBeanDesc(){
    wx.navigateTo({
      url: '/pages/userStatistics/Userindex/setBeanDesc',
    })
  },

  seletBalance(){
    let pay=this.data.pay;
    this.setData({
      pay:pay?false:true,
      isTime:false
    })
    this.getUserList()
  },
  seletTime(){
    let time=this.data.time;
    this.setData({
      time:time?false:true,
      isTime:true
    })
    this.getUserList()
  },

  //获取输入数据
  getSearchText(e){
    this.setData({
      inputText:e.detail.value
    })
    this.getUserList()
  },

  //获取今日时间段
  getTodayTime(){
      let newDate=new Date()
      let LastDate=new Date(new Date().getTime()+24*60*60*1000)
      let month=""
      let toDay=""
      let lastDay=""
      let startTime=""
      let endTime=""
      if(newDate.getMonth()+1<10){
        month="0"+(newDate.getMonth()+1)
      }else{
        month=newDate.getMonth()+1
      }
      if(newDate.getDate()<10){
        toDay="0"+newDate.getDate()
      }else{
        toDay=newDate.getDate()
      }
      
      if(LastDate.getDate()<10){
        lastDay="0"+LastDate.getDate()
      }else{
        lastDay=LastDate.getDate()
      }
  
      startTime=newDate.getFullYear()+"-"+month+"-"+toDay+" "+"00:00:00"
      endTime=newDate.getFullYear()+"-"+month+"-"+lastDay+" "+"00:00:00"

      this.setData({
        startTime:startTime,
        endTime:endTime
      })
  },


  getUserList(){
    let that =this
    let time =this.data.time
    let pay =this.data.pay
    let isTime=this.data.isTime
    let params={
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
    }
    params.pageNumber=this.data.pageNumber
    params.nick=""
    params.mobile=""
    let item = this.data.tabs[this.data.activeIndex];
    if(null!=item.vip){
      params.vip = item.vip;
    }
    if(this.data.activeIndex==0){
      params.vip = '';
    }

    if(this.data.activeIndex==1){
      params.isVip = 1;
    }


    if(this.data.activeIndex==0||this.data.activeIndex==1){
      params.startTime=this.data.startTime
      params.endTime=this.data.endTime
    }else{
      params.startTime=""
      params.endTime=""
    }

    if(this.data.activeIndex==1||this.data.activeIndex==3){
      params.vip = '';
      params.vips="1,2,3"
    }
    
    controllers.accountListPage(
      params
    ).then(res =>{

      // res.data.list.forEach(item=>{
      //   item.accountMobile=item.accountMobile.slice(0,3)+"****"+item.accountMobile.slice(7)
      // })

      that.setData({
        userList:res.data.list,
        lastPage:res.data.lastPage
      })
    })
  },

  getMore(){
    if(this.data.lastPage==false){
      let that =this
      let time =this.data.time
      let pay =this.data.pay
      let isTime=this.data.isTime
      let params={
        sessionId: wx.getStorageSync('token'),
        shopId:wx.getStorageSync('shopId'),
      }
      params.pageNumber=this.data.pageNumber+1
      this.setData({
        pageNumber:params.pageNumber
      })
      params.nick=""
      params.mobile=""
      let item = this.data.tabs[this.data.activeIndex];
      if(null!=item.vip){
        params.vip = item.vip;
      }

      if(this.data.activeIndex==0||this.data.activeIndex==1){
        params.startTime=this.data.startTime
        params.endTime=this.data.endTime
      }else{
        params.startTime=""
        params.endTime=""
      }

      if(this.data.activeIndex==1||this.data.activeIndex==3){
        params.vip = '';
        params.vips="1,2"
      }
      
      controllers.accountListPage(
        params
      ).then(res =>{
        let list=this.data.userList
        let list2=res.data.list
        let list3=list.concat(list2)
        that.setData({
          userList:list3,
          lastPage:res.data.lastPage
        })
      })
    }
  },

  clikcTabs(e) {
    const   index   = e.currentTarget.dataset.index
    
    this.setData({
      activeIndex:index
    });
    this.getUserList()
  },

  clikcDetails(res) {
   
    wx.navigateTo({
      url: '../UserDeatils/UserDeatils?id='+ res.currentTarget.dataset.id+"&accountId="+res.currentTarget.dataset.accountid
    })
  },

  //获取用户统计
  getUserStatistics(){
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    controllerOrder.getUserStatistics(value).then(res=>{
        this.setData({
          storeInfo:res.data
        })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getTodayTime()
    this.getUserList();
  },
  clikcDetails(res) {
   
    wx.navigateTo({
      url: '/pages/userStatistics/UserDeatils/UserDeatils?id='+ res.currentTarget.dataset.id+"&accountId="+res.currentTarget.dataset.accountid
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserStatistics()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})